using Microsoft.EntityFrameworkCore;
using ShopForWeb.Model.DBContexts;
using ShopForWeb.Model.Info;
using ShopForWeb.Model.View;
using ShopForWeb.Services;
using ShopForWeb.Services.Profiles;

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;

// Add services to the container.
#region 数据库上下文
services.AddDbContextPool<ShopDBContext>(options =>

{
  options.UseSqlServer(configuration.GetConnectionString("ShopDB"), b => b.MigrationsAssembly("ShopForWeb.UI"))
         .UseLazyLoadingProxies();  //启用延迟加载        
});
#endregion

#region 启用Session
services.Configure<CookiePolicyOptions>(options =>
{
  options.CheckConsentNeeded = context => true;
  options.MinimumSameSitePolicy = SameSiteMode.None;
});

//启用内存缓存(该步骤需在AddSession()调用前使用)
services.AddDistributedMemoryCache();

services.AddSession(options =>
{
  options.Cookie.Name = ".AdventureWorks.Session";
  options.IdleTimeout = TimeSpan.FromMinutes(20);//设置session的过期时间
  options.Cookie.HttpOnly = true;//设置在浏览器不能通过js获得该cookie的值
});

#endregion

services.AddScoped<CategoryService>();
services.AddScoped<ProductService>();
services.AddScoped<OrderService>();
services.AddScoped<OrderDetailService>();
services.AddScoped<UserService>();
services.AddScoped<OrderInfo>();
services.AddScoped<ShopCarService>();
services.AddAutoMapper(typeof(ShopProfile));

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "admin",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    
app.Run();
